package co.edu.usbcali.sam.presentation.backingBeans;

import java.io.Serializable;
import java.util.List;
import java.util.TimeZone;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.event.RowEditEvent;

import co.edu.usbcali.sam.exceptions.ZMessManager;
import co.edu.usbcali.sam.modelo.Calificaciones;
import co.edu.usbcali.sam.modelo.dto.CalificacionesDTO;
import co.edu.usbcali.sam.presentation.businessDelegate.IBusinessDelegatorView;
import co.edu.usbcali.sam.utilities.FacesUtils;


/**
 * @author Zathura Code Generator http://code.google.com/p/zathura
 * www.zathuracode.org
 *
 */
@ManagedBean
@ViewScoped
public class CalificacionesView implements Serializable {
    private static final long serialVersionUID = 1L;
    private InputText txtCorte1;
    private InputText txtCorte2;
    private InputText txtCorte3;
    private InputText txtDefinitiva;
    private InputText txtIdCali;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<CalificacionesDTO> data;
    private CalificacionesDTO selectedCalificaciones;
    private Calificaciones entity;
    private boolean showDialog;
    @ManagedProperty(value = "#{BusinessDelegatorView}")
    private IBusinessDelegatorView businessDelegatorView;

    public CalificacionesView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            CalificacionesDTO calificacionesDTO = (CalificacionesDTO) e.getObject();

            if (txtCorte1 == null) {
                txtCorte1 = new InputText();
            }

            txtCorte1.setValue(calificacionesDTO.getCorte1());

            if (txtCorte2 == null) {
                txtCorte2 = new InputText();
            }

            txtCorte2.setValue(calificacionesDTO.getCorte2());

            if (txtCorte3 == null) {
                txtCorte3 = new InputText();
            }

            txtCorte3.setValue(calificacionesDTO.getCorte3());

            if (txtDefinitiva == null) {
                txtDefinitiva = new InputText();
            }

            txtDefinitiva.setValue(calificacionesDTO.getDefinitiva());

            if (txtIdCali == null) {
                txtIdCali = new InputText();
            }

            txtIdCali.setValue(calificacionesDTO.getIdCali());

            Long idCali = FacesUtils.checkLong(txtIdCali);
            entity = businessDelegatorView.getCalificaciones(idCali);

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_new() {
        action_clear();
        selectedCalificaciones = null;
        setShowDialog(true);

        return "";
    }

    public String action_clear() {
        entity = null;
        selectedCalificaciones = null;

        if (txtCorte1 != null) {
            txtCorte1.setValue(null);
            txtCorte1.setDisabled(true);
        }

        if (txtCorte2 != null) {
            txtCorte2.setValue(null);
            txtCorte2.setDisabled(true);
        }

        if (txtCorte3 != null) {
            txtCorte3.setValue(null);
            txtCorte3.setDisabled(true);
        }

        if (txtDefinitiva != null) {
            txtDefinitiva.setValue(null);
            txtDefinitiva.setDisabled(true);
        }

        if (txtIdCali != null) {
            txtIdCali.setValue(null);
            txtIdCali.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        return "";
    }

    public void listener_txtId() {
        try {
            Long idCali = FacesUtils.checkLong(txtIdCali);
            entity = (idCali != null)
                ? businessDelegatorView.getCalificaciones(idCali) : null;
        } catch (Exception e) {
            entity = null;
        }

        if (entity == null) {
            txtCorte1.setDisabled(false);
            txtCorte2.setDisabled(false);
            txtCorte3.setDisabled(false);
            txtDefinitiva.setDisabled(false);
            txtIdCali.setDisabled(false);
            btnSave.setDisabled(false);
        } else {
            txtCorte1.setValue(entity.getCorte1());
            txtCorte1.setDisabled(false);
            txtCorte2.setValue(entity.getCorte2());
            txtCorte2.setDisabled(false);
            txtCorte3.setValue(entity.getCorte3());
            txtCorte3.setDisabled(false);
            txtDefinitiva.setValue(entity.getDefinitiva());
            txtDefinitiva.setDisabled(false);
            txtIdCali.setValue(entity.getIdCali());
            txtIdCali.setDisabled(true);
            btnSave.setDisabled(false);

            if (btnDelete != null) {
                btnDelete.setDisabled(false);
            }
        }
    }

    public String action_edit(ActionEvent evt) {
        selectedCalificaciones = (CalificacionesDTO) (evt.getComponent()
                                                         .getAttributes()
                                                         .get("selectedCalificaciones"));
        txtCorte1.setValue(selectedCalificaciones.getCorte1());
        txtCorte1.setDisabled(false);
        txtCorte2.setValue(selectedCalificaciones.getCorte2());
        txtCorte2.setDisabled(false);
        txtCorte3.setValue(selectedCalificaciones.getCorte3());
        txtCorte3.setDisabled(false);
        txtDefinitiva.setValue(selectedCalificaciones.getDefinitiva());
        txtDefinitiva.setDisabled(false);
        txtIdCali.setValue(selectedCalificaciones.getIdCali());
        txtIdCali.setDisabled(true);
        btnSave.setDisabled(false);
        setShowDialog(true);

        return "";
    }

    public String action_save() {
        try {
            if ((selectedCalificaciones == null) && (entity == null)) {
                action_create();
            } else {
                action_modify();
            }

            data = null;
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_create() {
        try {
            entity = new Calificaciones();

            Long idCali = FacesUtils.checkLong(txtIdCali);

            entity.setCorte1(FacesUtils.checkDouble(txtCorte1));
            entity.setCorte2(FacesUtils.checkDouble(txtCorte2));
            entity.setCorte3(FacesUtils.checkDouble(txtCorte3));
            entity.setDefinitiva(FacesUtils.checkDouble(txtDefinitiva));
            entity.setIdCali(idCali);
            businessDelegatorView.saveCalificaciones(entity);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            entity = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            if (entity == null) {
                Long idCali = new Long(selectedCalificaciones.getIdCali());
                entity = businessDelegatorView.getCalificaciones(idCali);
            }

            entity.setCorte1(FacesUtils.checkDouble(txtCorte1));
            entity.setCorte2(FacesUtils.checkDouble(txtCorte2));
            entity.setCorte3(FacesUtils.checkDouble(txtCorte3));
            entity.setDefinitiva(FacesUtils.checkDouble(txtDefinitiva));
            businessDelegatorView.updateCalificaciones(entity);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete_datatable(ActionEvent evt) {
        try {
            selectedCalificaciones = (CalificacionesDTO) (evt.getComponent()
                                                             .getAttributes()
                                                             .get("selectedCalificaciones"));

            Long idCali = new Long(selectedCalificaciones.getIdCali());
            entity = businessDelegatorView.getCalificaciones(idCali);
            action_delete();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete_master() {
        try {
            Long idCali = FacesUtils.checkLong(txtIdCali);
            entity = businessDelegatorView.getCalificaciones(idCali);
            action_delete();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public void action_delete() throws Exception {
        try {
            businessDelegatorView.deleteCalificaciones(entity);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data = null;
        } catch (Exception e) {
            throw e;
        }
    }

    public String action_closeDialog() {
        setShowDialog(false);
        action_clear();

        return "";
    }

    public String actionDeleteDataTableEditable(ActionEvent evt) {
        try {
            selectedCalificaciones = (CalificacionesDTO) (evt.getComponent()
                                                             .getAttributes()
                                                             .get("selectedCalificaciones"));

            Long idCali = new Long(selectedCalificaciones.getIdCali());
            entity = businessDelegatorView.getCalificaciones(idCali);
            businessDelegatorView.deleteCalificaciones(entity);
            data.remove(selectedCalificaciones);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(Double corte1, Double corte2,
        Double corte3, Double definitiva, Long idCali)
        throws Exception {
        try {
            entity.setCorte1(FacesUtils.checkDouble(corte1));
            entity.setCorte2(FacesUtils.checkDouble(corte2));
            entity.setCorte3(FacesUtils.checkDouble(corte3));
            entity.setDefinitiva(FacesUtils.checkDouble(definitiva));
            businessDelegatorView.updateCalificaciones(entity);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("CalificacionesView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtCorte1() {
        return txtCorte1;
    }

    public void setTxtCorte1(InputText txtCorte1) {
        this.txtCorte1 = txtCorte1;
    }

    public InputText getTxtCorte2() {
        return txtCorte2;
    }

    public void setTxtCorte2(InputText txtCorte2) {
        this.txtCorte2 = txtCorte2;
    }

    public InputText getTxtCorte3() {
        return txtCorte3;
    }

    public void setTxtCorte3(InputText txtCorte3) {
        this.txtCorte3 = txtCorte3;
    }

    public InputText getTxtDefinitiva() {
        return txtDefinitiva;
    }

    public void setTxtDefinitiva(InputText txtDefinitiva) {
        this.txtDefinitiva = txtDefinitiva;
    }

    public InputText getTxtIdCali() {
        return txtIdCali;
    }

    public void setTxtIdCali(InputText txtIdCali) {
        this.txtIdCali = txtIdCali;
    }

    public List<CalificacionesDTO> getData() {
        try {
            if (data == null) {
                data = businessDelegatorView.getDataCalificaciones();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<CalificacionesDTO> calificacionesDTO) {
        this.data = calificacionesDTO;
    }

    public CalificacionesDTO getSelectedCalificaciones() {
        return selectedCalificaciones;
    }

    public void setSelectedCalificaciones(CalificacionesDTO calificaciones) {
        this.selectedCalificaciones = calificaciones;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }

    public IBusinessDelegatorView getBusinessDelegatorView() {
        return businessDelegatorView;
    }

    public void setBusinessDelegatorView(
        IBusinessDelegatorView businessDelegatorView) {
        this.businessDelegatorView = businessDelegatorView;
    }

    public boolean isShowDialog() {
        return showDialog;
    }

    public void setShowDialog(boolean showDialog) {
        this.showDialog = showDialog;
    }
}
